package com.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.dao.DynamicDao;
import com.entity.Dynamic;

/**
 * @description:
 * @author:wsc
 * @date:2020年3月8日 下午4:02:36
 */
@Repository
public class DynamicDaoImpl implements DynamicDao{
	@Resource
	public SessionFactory sessionFactory;

	@Override
	public boolean addDynamic(Dynamic dynamic) {
		this.sessionFactory.getCurrentSession().save(dynamic);
		return false;
	}

	@Override
	public void deleteDynamic(Dynamic dynamic) {
		this.sessionFactory.getCurrentSession().delete(dynamic);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicDescByUid(int uid) {
		String hql="from Dynamic where uid=:uid order by releaseTime desc";
		Query<Dynamic> query=this.sessionFactory.getCurrentSession().createQuery(hql).setParameter("uid", uid);
		List<Dynamic> list=query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicAscByUid(int uid) {
		String hql="from Dynamic where uid=:uid order by releaseTime asc";
		Query<Dynamic> query=this.sessionFactory.getCurrentSession().createQuery(hql).setParameter("uid", uid);
		List<Dynamic> list=query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicDescByPage(int pageNum, int pageSize) {
		String hql="from Dynamic order by releaseTime desc";
		return sessionFactory.getCurrentSession().createQuery(hql).setFirstResult((pageNum-1)*pageSize).setMaxResults(pageSize).list();
	}

	@Override
	public void updateDynamic(Dynamic dynamic) {
		this.sessionFactory.getCurrentSession().update(dynamic);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicList() {
		String hql="from Dynamic";
		Query<Dynamic> query=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<Dynamic> list=query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicPageDescByUid(int uid, int pageNum, int pageSize) {
		String hql="from Dynamic where uid=:uid order by releaseTime desc";
		return sessionFactory.getCurrentSession().createQuery(hql).setParameter("uid", uid).setFirstResult((pageNum-1)*pageSize).setMaxResults(pageSize).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dynamic> queryDynamicPageAscByUid(int uid, int pageNum, int pageSize) {
		String hql="from Dynamic where uid=:uid order by releaseTime asc";
		return sessionFactory.getCurrentSession().createQuery(hql).setParameter("uid", uid).setFirstResult((pageNum-1)*pageSize).setMaxResults(pageSize).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Dynamic queryDynamicByDid(int did) {
		Query<Dynamic> query = sessionFactory.getCurrentSession().createQuery("from Dynamic where did=:did");
		query.setParameter("did", did);
		Dynamic dynamic = (Dynamic) query.uniqueResult();
		return dynamic;
	}

}
